package cn.wolfcode.wolf2w.util;


import cn.wolfcode.wolf2w.exception.LogicException;
import org.springframework.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class AsserUtil {
    /**
     * 判断值是否为空,如果为空,则抛异常,异常信息为msg
     *
     * @param val
     * @param msg
     */
    public static void hasLength(String val, String msg) {
        if (!StringUtils.hasLength(val)) {
            //如果为空,抛异常
            throw new LogicException(msg);
        }
    }

    /**
     * 比较两个值是否相等,不相等则返回异常信息
     *
     * @param password
     * @param rpassword
     */
    public static boolean isEquals(String password, String rpassword, String msg) {
        if (password == null || !password.equals(rpassword)) {
            throw new LogicException(msg);
        }
        return true;
    }

    public static void checkMobileNumber(String phone) {
        boolean flag = false;
        try {
            Pattern regex = Pattern.compile("^(((13[0-9])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8})|(0\\d{2}-\\d{8})|(0\\d{3}-\\d{7})$");
            Matcher matcher = regex.matcher(phone);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        if (flag==false){
            throw new LogicException("手机格式错误");
        }
    }
}
